﻿using System;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;

namespace ResourceReportBuilder
{
    internal static class FileUtils
    {
        internal static Icon GetAssociatedIcon(string path)
        {
            ResourceReportBuilder.Win32.SHFILEINFO shinfo = new ResourceReportBuilder.Win32.SHFILEINFO();
            IntPtr hImg = Win32.SHGetFileInfo(path, 0, ref shinfo,
              (uint)Marshal.SizeOf(shinfo),
               Win32.SHGFI_ICON |
               Win32.SHGFI_SMALLICON);
            Icon fileIcon = null;
            if (shinfo.hIcon != IntPtr.Zero)
            {
                fileIcon = Icon.FromHandle(shinfo.hIcon);
            }
            return fileIcon;
        }

        internal static bool IsRoot(string path)
        {
            DirectoryInfo di = new DirectoryInfo(path);

            int count = 0;
            for (int i = 0; i < path.Length; i++)
            {
                if (path[i] == '\\')
                {
                    count++;
                }
            }

            return path == di.Root.FullName || (count == 2 && path.Contains(".."));
        }
    }
}
